[C#]How to introduce retry logic into LINQ to SQL to deal with timeouts?

Posted by codemonkie on Stack Overflow See other posts from Stack Overflow or by codemonkie
Published on 2010-04-07T11:04:36Z Indexed on 2010/04/07 11:13 UTC
Read the original article Hit count: 235

Filed under:
|
|

I need to find ways to add retry mechanism to my DB calls in case of timeouts, LINQ to SQL is used to call some sprocs in my code...

using (MyDataContext dc = new MyDataContext())
{
    int result = -1; //denote failure
    int count = 0;

    while ((result < 0) && (count < MAX_RETRIES))
    {
        result = dc.myStoredProc1(...);
        count++;
    }

    result = -1;
    count  = 0;
    while ((result < 0) && (count < MAX_RETRIES))
    {
        result = dc.myStoredProc2(...);
        count++;
    }

    ...

    ...
}

Not sure if the code above is right or posed any complications.

It'll be nice to throw an exception after MAX_RETRIES has reached, but I dunno how and where to throw them appropriately :-)

Any helps appreciated.

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about c#